create Moloco Banner
Creates a banner ad object of the requested BannerAdSize using the provided ad unit ID. Optionally, a watermark string can be added to the ad. The result of the ad creation process is returned through the provided callback, which will either contain the created ad object or an error describing the failure. NOTE - The API requires Moloco.initialize to have been called.
This is the unified entry point for every banner format. The size parameter selects the container:
BannerAdSize.Standard - standard banner, container size 320x50
BannerAdSize.Tablet - tablet leaderboard banner, container size 728x90
BannerAdSize.MREC - medium rectangle, container size 300x250
BannerAdSize.InlineAdaptive - inline adaptive banner
BannerAdSize.AnchoredAdaptive - anchored adaptive banner
Both adaptive variants let the server determine the ad height from the bid response (creative height). By default the banner container fills its parent's width (MATCH_PARENT). Supplying availableWidth (in dp) instead pins the container to that fixed width. It is recommended to pass availableWidth for adaptive bidding and set it to the same wMax used in your adaptive / flex-slot bid request, so the requested and rendered widths match. For adaptive sizes, host the returned Banner in a container with a WRAP_CONTENT height (or the known bid response / creative height) so the creative is not clipped.
Parameters
Mediation adapter calling Moloco SDK
The unique identifier for the ad unit to load the ad from. This must be a valid ad unit ID
The BannerAdSize describing which banner container to create
An optional string that, if provided, will be displayed as a watermark on the ad. If null, no watermark will be applied.
A callback function that will be invoked when the ad creation process is complete. It returns either a valid Banner ad object if successful, or a AdCreateError indicating the type of error that occurred.
Samples
import android.content.Context
import android.widget.FrameLayout
import com.moloco.sdk.internal.MolocoLogger
import com.moloco.sdk.publisher.MolocoAdError.AdCreateError
import com.moloco.sdk.publisher.init.MolocoInitParams
fun main() {
//sampleStart
// createXXX calls requires [Moloco.initialize] to have been invoked
// Standard banner (320x50). Swap [BannerAdSize] for the format you need:
// BannerAdSize.Tablet (728x90) or BannerAdSize.MREC (300x250).
Moloco.createMolocoBanner(
mediationInfo = MediationInfo("MY_MEDIATION"),
adUnitId = "MOLOCO_ADUNIT_ID",
size = BannerAdSize.Standard,
) { banner: Banner?, adCreateError: AdCreateError? ->
if (banner != null) {
banner.load("bid_response", listener = null)
frameLayout.addView(banner)
// ...
banner.destroy()
frameLayout.removeView(banner)
} else {
// Use the `adCreateError` to determine the error
}
}
// createXXX calls requires [Moloco.initialize] to have been invoked
// Inline adaptive banner. Width fills the container, height comes from the bid response (creative height).
// Recommended for adaptive bidding: pass availableWidth (dp) and match it to the wMax used in
// the adaptive / flex-slot bid request. Omit it to fill the parent width.
Moloco.createMolocoBanner(
mediationInfo = MediationInfo("MY_MEDIATION"),
adUnitId = "MOLOCO_ADUNIT_ID",
size = BannerAdSize.InlineAdaptive(availableWidth = 320),
) { banner: Banner?, adCreateError: AdCreateError? ->
if (banner != null) {
banner.load("bid_response", listener = null)
frameLayout.addView(banner)
// ...
banner.destroy()
frameLayout.removeView(banner)
} else {
// Use the `adCreateError` to determine the error
}
}
// createXXX calls requires [Moloco.initialize] to have been invoked
// Anchored adaptive banner. Same rendering as inline.
// Here availableWidth is omitted (defaults to null), so the container fills its parent width.
Moloco.createMolocoBanner(
mediationInfo = MediationInfo("MY_MEDIATION"),
adUnitId = "MOLOCO_ADUNIT_ID",
size = BannerAdSize.AnchoredAdaptive(),
) { banner: Banner?, adCreateError: AdCreateError? ->
if (banner != null) {
banner.load("bid_response", listener = null)
frameLayout.addView(banner)
// ...
banner.destroy()
frameLayout.removeView(banner)
} else {
// Use the `adCreateError` to determine the error
}
}
//sampleEnd
}